home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2002 November / SGI IRIX Base Documentation 2002 November.iso / usr / share / catman / p_man / cat3 / librw / RWTPtrHashTable.z / RWTPtrHashTable
Encoding:
Text File  |  2002-10-03  |  12.2 KB  |  331 lines

  1.  
  2.  
  3.  
  4. RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhTTTTaaaabbbblllleeee((((3333CCCC++++++++))))                                    RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhTTTTaaaabbbblllleeee((((3333CCCC++++++++))))
  5.  
  6.  
  7.  
  8. NNNNaaaammmmeeee
  9.      RWTPtrHashTable<T> - Rogue Wave library class
  10.  
  11. SSSSyyyynnnnooooppppssssiiiissss
  12.               #include <rw/tphasht.h>
  13.  
  14.  
  15.  
  16.               unsigned hashFun(const T&);
  17.           RWTPtrHashTable<T> table(hashFun);
  18.  
  19. PPPPlllleeeeaaaasssseeee NNNNooootttteeee!!!!
  20.      IIIIffff yyyyoooouuuu ddddoooo nnnnooootttt hhhhaaaavvvveeee tttthhhheeee SSSSttttaaaannnnddddaaaarrrrdddd CCCC++++++++ LLLLiiiibbbbrrrraaaarrrryyyy,,,, uuuusssseeee tttthhhheeee iiiinnnntttteeeerrrrffffaaaacccceeee ddddeeeessssccccrrrriiiibbbbeeeedddd
  21.      hhhheeeerrrreeee....  OOOOtttthhhheeeerrrrwwwwiiiisssseeee,,,, uuuusssseeee tttthhhheeee iiiinnnntttteeeerrrrffffaaaacccceeee ttttoooo RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhMMMMuuuullllttttiiiiSSSSeeeetttt described in
  22.      the Class Reference.
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. DDDDeeeessssccccrrrriiiippppttttiiiioooonnnn
  30.      This class implements a parameterized hash table of types T.  It uses
  31.      chaining to resolve hash collisions.  Duplicates are allowed.  It is a
  32.      ppppooooiiiinnnntttteeeerrrr based collection: pointers to objects are copied in and out of
  33.      the hash buckets. Parameter TTTT represents the type of object to be
  34.      inserted into the table, either a class or fundamental type.  The class TTTT
  35.      must have:
  36.           well-defined equality semantics (TTTT::::::::ooooppppeeeerrrraaaattttoooorrrr========((((ccccoooonnnnsssstttt TTTT&&&&))))).
  37.  
  38.      A user-supplied hashing function for type TTTT must be supplied to the
  39.      constructor when creating a new table.  If TTTT is a Rogue Wave class, then
  40.      this requirement is usually trivial because most Rogue Wave objects know
  41.      how to return a hashing value.  In fact, classes RRRRWWWWCCCCSSSSttttrrrriiiinnnngggg, RRRRWWWWDDDDaaaatttteeee,
  42.      RRRRWWWWTTTTiiiimmmmeeee, and RRRRWWWWWWWWSSSSttttrrrriiiinnnngggg contain static member functions called hhhhaaaasssshhhh that
  43.      can be supplied to the constructor as is.  The function must have
  44.      prototype:.Ex
  45.          unsigned hhhhFFFFuuuunnnn(const T& a);
  46.  
  47.  
  48.  
  49.  
  50.  
  51.      and should return a suitable hash value for the object aaaa. To find an
  52.      object, it is first hashed to determine in which bucket it occurs. The
  53.      bucket is then searched for an object that is equal (as determined by the
  54.      equality operator) to the candidate.  The initial number of buckets in
  55.      the table is set by the constructor.  There is a default value.  If the
  56.      number of items in the collection greatly exceeds the number of buckets
  57.      then efficiency will sag because each bucket must be searched linearly.
  58.      The number of buckets can be changed by calling member function rrrreeeessssiiiizzzzeeee(((()))).
  59.      This is relatively expensive because all of the keys must be rehashed. If
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhTTTTaaaabbbblllleeee((((3333CCCC++++++++))))                                    RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhTTTTaaaabbbblllleeee((((3333CCCC++++++++))))
  71.  
  72.  
  73.  
  74.      you wish for this to be done automatically, then you can subclass from
  75.      this class and implement your own special iiiinnnnsssseeeerrrrtttt(((()))) and rrrreeeemmmmoooovvvveeee(((()))) functions
  76.      which perform a rrrreeeessssiiiizzzzeeee(((()))) as necessary.
  77.  
  78. PPPPeeeerrrrssssiiiisssstttteeeennnncccceeee
  79.      None
  80.  
  81. EEEExxxxaaaammmmpppplllleeee
  82.               #include <rw/tphasht.h>
  83.           #include <rw/cstring.h>
  84.           #include <rw/rstream.h>
  85.           main()  {
  86.             RWTPtrHashTable<RWCString> table(RWCString::hash);
  87.             RWCString *states[4] = { new RWCString("Alabama"),
  88.                                      new RWCString("Pennsylvania"),
  89.                                      new RWCString("Oregon"),
  90.                                      new RWCString("Montana") };
  91.             table.insert(states[0]);
  92.             table.insert(states[1]);
  93.             table.insert(states[2]);
  94.             table.insert(states[3]);
  95.             RWCString key("Oregon");
  96.             cout << "The table " <<
  97.               (table.contains(&key) ? "does " : "does not ") <<
  98.               "contain Oregon0;
  99.             table.removeAll(&key);
  100.             cout << "Now the table " <<
  101.               (table.contains(&key) ? "does " : "does not ") <<
  102.               "contain Oregon";
  103.             delete states[0];
  104.             delete states[1];
  105.             delete states[2];
  106.             delete states[3];
  107.             return 0;
  108.           }
  109.  
  110.  
  111.      Program output
  112.  
  113.               The table does contain Oregon
  114.           Now the table does not contain Oregon
  115.  
  116.  
  117.  
  118.               .Ex
  119.  
  120.  
  121. PPPPuuuubbbblllliiiicccc CCCCoooonnnnssssttttrrrruuuuccccttttoooorrrrssss
  122.               RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhTTTTaaaabbbblllleeee<T>(unsigned (*hashFun)(const T&),
  123.                                  size_t buckets = RWDEFAULT_CAPACITY);
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhTTTTaaaabbbblllleeee((((3333CCCC++++++++))))                                    RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhTTTTaaaabbbblllleeee((((3333CCCC++++++++))))
  137.  
  138.  
  139.  
  140.      Constructs an empty hash table.  The first argument is a pointer to a
  141.      user-defined hashing function for items of type TTTT.... The table will
  142.      initally have bbbbuuuucccckkkkeeeettttssss buckets although this can be changed with member
  143.      function rrrreeeessssiiiizzzzeeee(((()))).
  144.  
  145.               RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhTTTTaaaabbbblllleeee<T>(const RWTPtrHashTable<T>& c);
  146.  
  147.  
  148.      Constructs a new hash table as a shallow copy of cccc.  After construction,
  149.      pointers will be shared between the two collections.  The new object will
  150.      have the same number of buckets as cccc.  Hence, the keys will not be
  151.      rehashed.
  152.  
  153. PPPPuuuubbbblllliiiicccc OOOOppppeeeerrrraaaattttoooorrrrssss
  154.               RWTPtrHashTable&
  155.           ooooppppeeeerrrraaaattttoooorrrr====(const RWTPtrHashTable<T>& c);
  156.  
  157.  
  158.      Sets self to a shallow copy of cccc.  Afterwards, pointers will be shared
  159.      between the two collections and self will have the same number of buckets
  160.      as cccc.  Hence, the keys will not be rehashed.
  161.  
  162.               void
  163.  
  164.  
  165.  
  166. PPPPuuuubbbblllliiiicccc MMMMeeeemmmmbbbbeeeerrrr FFFFuuuunnnnccccttttiiiioooonnnnssss
  167.               aaaappppppppllllyyyy(void (*applyFun)(T*, void*), void* d);
  168.  
  169.  
  170.      Applies the user-defined function pointed to by aaaappppppppllllyyyyFFFFuuuunnnn to every item in
  171.      the table.  This function must have prototype:
  172.  
  173.               void yyyyoooouuuurrrrFFFFuuuunnnn(T* a, void* d);
  174.  
  175.  
  176.  
  177.  
  178.  
  179.      Client data may be passed through as parameter dddd.  The items should not
  180.      be changed in any way that could change their hash value.
  181.  
  182.               void
  183.           cccclllleeeeaaaarrrr();
  184.  
  185.  
  186.      Removes all items from the collection.
  187.  
  188.               void
  189.           cccclllleeeeaaaarrrrAAAAnnnnddddDDDDeeeessssttttrrrrooooyyyy();
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhTTTTaaaabbbblllleeee((((3333CCCC++++++++))))                                    RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhTTTTaaaabbbblllleeee((((3333CCCC++++++++))))
  203.  
  204.  
  205.  
  206.      Removes all items from the collection aaaannnndddd deletes them.
  207.  
  208.               RWBoolean
  209.           ccccoooonnnnttttaaaaiiiinnnnssss(const T* p) const;
  210.  
  211.  
  212.      Returns TTTTRRRRUUUUEEEE if the collection contains an item which is equal to the
  213.      item pointed to by pppp.  Returns FFFFAAAALLLLSSSSEEEE otherwise.  Equality is measured by
  214.      the class-defined equality operator for type TTTT.
  215.  
  216.               size_t
  217.           eeeennnnttttrrrriiiieeeessss() const;
  218.  
  219.  
  220.      Returns the number of items currently in the collection.
  221.  
  222.               T*
  223.           ffffiiiinnnndddd(const T* target) const;
  224.  
  225.  
  226.      Returns a pointer to the object which is equal to the object pointed to
  227.      by ttttaaaarrrrggggeeeetttt, or nnnniiiillll if no such object can be found.  Equality is measured
  228.      by the class-defined equality operator for type TTTT.
  229.  
  230.               void
  231.           iiiinnnnsssseeeerrrrtttt(T* a);
  232.  
  233.  
  234.      Adds the object pointed to by aaaa to the collection.
  235.  
  236.               RWBoolean
  237.           iiiissssEEEEmmmmppppttttyyyy() const;
  238.  
  239.  
  240.      Returns TTTTRRRRUUUUEEEE if the collection has no items in it, FFFFAAAALLLLSSSSEEEE otherwise.
  241.  
  242.               size_t
  243.           ooooccccccccuuuurrrrrrrreeeennnncccceeeessssOOOOffff(const T* a) const;
  244.  
  245.  
  246.      Returns the number of objects in the collection which are equal to the
  247.      object pointed to by aaaa.  Equality is measured by the class-defined
  248.      equality operator for type TTTT.
  249.  
  250.               T*
  251.           rrrreeeemmmmoooovvvveeee(const T* a);
  252.  
  253.  
  254.      Removes the object which is equal to the object pointed to by aaaa and
  255.      returns a pointer to it, or nnnniiiillll if no such object could be found.
  256.      Equality is measured by the class-defined equality operator for type TTTT.
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhTTTTaaaabbbblllleeee((((3333CCCC++++++++))))                                    RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhTTTTaaaabbbblllleeee((((3333CCCC++++++++))))
  269.  
  270.  
  271.  
  272.               size_t
  273.           rrrreeeemmmmoooovvvveeeeAAAAllllllll(const T* a);
  274.  
  275.  
  276.      Removes all objects which are equal to the object pointed to by aaaa.
  277.      Returns the number of objects removed.  Equality is measured by the
  278.      class-defined equality operator for type TTTT.
  279.  
  280.               void
  281.           rrrreeeessssiiiizzzzeeee(size_t N);
  282.  
  283.  
  284.      Changes the number of buckets to NNNN.  This will result in all of the
  285.      objects in the collection being rehashed.
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.